package com.changgou.goods.controller;

import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.changgou.util.Result;
import com.changgou.util.StatusCode;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/brand")
@CrossOrigin//跨域
public class BrandController {
   @Autowired
   private BrandService brandService;
    /**
     * 用于查询所有的品牌
     */
    @GetMapping
    public Result<Brand> findAll(){
        List<Brand> brands = brandService.findAll();
        return new Result<>(true, StatusCode.OK,"查询所有商品成功",brands);
    }

    /**
     * 根据id查询品牌
     */
    @GetMapping(value="/{id}")
    public Result findById(@PathVariable(value ="id") Integer id){
        Brand brand= brandService.findById(id);
        return new Result(true,StatusCode.OK,"查询商品成功",brand);
    }

    /**
     * 新增品牌
     */
    @PostMapping
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true,StatusCode.OK,"添加品牌成功");
    }

    /**
     * 修改品牌
     */
    @PostMapping(value = "/{id}")
    public Result update(@RequestBody Brand brand,@PathVariable(value = "id") Integer id){
        //设置ID
        brand.setId(id);
        //修改数据
        brandService.update(brand);
        return  new Result(true,StatusCode.OK,"修改品牌数据成功");
    }

    /**
     * 删除品牌信息
     */
    @DeleteMapping(value="/{id}")
    public Result delete(@PathVariable(value="id") Integer id){
        brandService.delete(id);
        return new Result(true,StatusCode.OK,"删除商品信息成功");
    }

    /**
     * 多条件查询
     */
    @PostMapping(value="/search")
    public Result<List<Brand>> findList(@RequestBody Brand brand){
        List<Brand> list = brandService.findList(brand);
        return new Result<List<Brand>>(true,StatusCode.OK,"多条件查询成功",list);
    }

    /**
     * 分页查询
     */
    @GetMapping(value="/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable (value = "page") Integer page
                           ,@PathVariable(value="size") Integer size){
        PageInfo<Brand> page1 = brandService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"分页查询成功",page1);
    }

    /**
     * 分页条件查询
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo>findpage(@RequestBody Brand brand,@PathVariable (value = "page") Integer page
            ,@PathVariable(value="size") Integer size){
        PageInfo<Brand> page1 = brandService.findPageAndList(brand,page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"分页查询成功",page1);
    }

}
